package com.cws.project.cws.signinstudent.controller;

import java.util.List;
import java.util.stream.Collectors;

import com.cws.common.utils.DateUtils;
import com.cws.common.utils.StringUtils;
import com.cws.common.utils.poi.ExcelUtil;
import com.cws.framework.aspectj.lang.annotation.Log;
import com.cws.framework.aspectj.lang.enums.BusinessType;
import com.cws.framework.web.controller.BaseController;
import com.cws.framework.web.domain.AjaxResult;
import com.cws.framework.web.page.TableDataInfo;
import com.cws.project.cws.clazz.domain.ClazzUser;
import com.cws.project.cws.clazz.service.IClazzUserService;
import com.cws.project.cws.course.domain.Course;
import com.cws.project.cws.course.service.ICourseService;
import com.cws.project.cws.signin.domain.SignIn;
import com.cws.project.cws.signin.service.ISignInService;
import com.cws.project.system.user.domain.User;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.cws.project.cws.signinstudent.domain.SignInStudent;
import com.cws.project.cws.signinstudent.service.ISignInStudentService;

/**
 * 学生签到记录Controller
 *
 * @author xyl
 * @date 2022-03-27
 */
@Controller
@RequestMapping("/cws/signinstudent")
public class SignInStudentController extends BaseController {
    private String prefix = "cws/signinstudent";

    @Autowired
    private ISignInStudentService signInStudentService;
    @Autowired
    private ICourseService courseService;
    @Autowired
    private ISignInService signInService;
    @Autowired
    private IClazzUserService clazzUserService;

    @RequiresPermissions("cws:signinstudent:view")
    @GetMapping()
    public String signinstudent() {
        return prefix + "/signinstudent";
    }

    /**
     * 查询学生签到记录列表
     */
    @RequiresPermissions("cws:signinstudent:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(SignInStudent signInStudent) {
        User sysUser = getSysUser();
        //辅导员 查看自己班的
        if(sysUser.isInstructor()){
            ClazzUser clazzUser = new ClazzUser();
            clazzUser.setUserId(sysUser.getUserId());
            signInStudent.setClazzIds(clazzUserService.selectClazzUserList(clazzUser).stream().map(ClazzUser::getClazzId).collect(Collectors.toList()));
        }
        //教师
        if(sysUser.isTeacher()){
            signInStudent.setTeacherId(sysUser.getUserId());
        }
        if(sysUser.isStudent()){
            signInStudent.setStudentId(getUserId());
        }
        startPage();
        List<SignInStudent> list = signInStudentService.selectSignInStudentList(signInStudent);
        return getDataTable(list);
    }

    /**
     * 导出学生签到记录列表
     */
    @RequiresPermissions("cws:signinstudent:export")
    @Log(title = "学生签到记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(SignInStudent signInStudent) {
        List<SignInStudent> list = signInStudentService.selectSignInStudentList(signInStudent);
        ExcelUtil<SignInStudent> util = new ExcelUtil<SignInStudent>(SignInStudent.class);
        return util.exportExcel(list, "signinstudent");
    }

    /**
     * 新增学生签到记录
     */
    @GetMapping("/add")
    public String add() {
        return prefix + "/add";
    }

    /**
     * 新增保存学生签到记录
     */
    @RequiresPermissions("cws:signinstudent:add")
    @Log(title = "学生签到记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(SignInStudent signInStudent) {
        signInStudent.setStudentId(getUserId());
        signInStudent.setSignInTime(DateUtils.getNowDate());
        //查询是否有 符合的签到
        SignIn conform = signInService.selectSignInConform(signInStudent.getCourseId(),signInStudent.getSignInTime());
        if(conform == null){
            return error("签到未开始 或 已经结束！");
        }
        SignIn signIn = signInService.selectSignInNow(signInStudent.getCourseId());
        if (!StringUtils.equalsIgnoreCase(signIn.getVerifyCode(),signInStudent.getVerifyCode())) {
            return error("校验码错误！");
        }

        signInStudent.setSignInId(conform.getSignInId());
        SignInStudent s =  signInStudentService.selectSignInStudent(signInStudent);
        if(s != null){
           return error("请勿重复签到！");
        }

        return toAjax(signInStudentService.insertSignInStudent(signInStudent));
    }

    /**
     * 修改学生签到记录
     */
    @GetMapping("/edit/{courseId}")
    public String edit(@PathVariable("courseId") Long courseId, ModelMap mmap) {
        Course course = courseService.selectCourseById(courseId);
        mmap.put("course", course);
        return prefix + "/edit";
    }

    /**
     * 修改保存学生签到记录
     */
    @RequiresPermissions("cws:signinstudent:edit")
    @Log(title = "学生签到记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(SignInStudent signInStudent) {
        return toAjax(signInStudentService.updateSignInStudent(signInStudent));
    }

    /**
     * 删除学生签到记录
     */
    @RequiresPermissions("cws:signinstudent:remove")
    @Log(title = "学生签到记录", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids) {
        return toAjax(signInStudentService.deleteSignInStudentByIds(ids));
    }
}
